var esc = require("./ble/esc.js");
var bleSend={
	looptime:1,
	currentTime:1,
	BLEInformation:{},
	//准备发送，根据每次发送字节数来处理分包数量
	prepareSend:function(buff,BLEInformation) {
		console.log(buff);
		let that = this
		let time = 20
		let looptime = parseInt(buff.length / time);
		let lastData = parseInt(buff.length % time);
		console.log(looptime + "---" + lastData)
		this.looptime = looptime + 1;
		this.BLEInformation = {
			deviceId:BLEInformation.split('-%-')[0],
			serviceId:BLEInformation.split('-%-')[2],
			characteristicId:BLEInformation.split('-%-')[3]
		}
		this.lastData = lastData;
		this.currentTime = 1;
		that.Send(buff)
	},
	Send:function(buff){
		let that = this
		let {
			looptime: loopTime,
			currentTime,
			lastData,
			BLEInformation
		} = that;
		let onTimeData = 20,
			printNum = 1,
			currentPrint = 1
		let buf;
		let dataView;
		if (currentTime < loopTime) {
			buf = new ArrayBuffer(onTimeData)
			dataView = new DataView(buf)
			for (var i = 0; i < onTimeData; ++i) {
				dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
			}
		} else {
			buf = new ArrayBuffer(lastData)
			dataView = new DataView(buf)
			for (var i = 0; i < lastData; ++i) {
				dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
			}
		}
		console.log("第" + currentTime + "次发送数据大小为：" + buf.byteLength)
		// let {
		// 	BLEInformation
		// } = that.Bluetooth;
		uni.writeBLECharacteristicValue({
			deviceId: BLEInformation.deviceId,
			serviceId: BLEInformation.serviceId,
			characteristicId: BLEInformation.characteristicId,
			value: buf,
			success: function(res) {
				console.log(res)
			},
			fail: function(e) {
				console.log(e)
			},
			complete: function() {
				currentTime++
				if (currentTime <= loopTime) {
					that.currentTime = currentTime;
					that.Send(buff)
				} else {
					// uni.showToast({
					// 	title: '已打印第' + currentPrint + '张',
					// })
					if (currentPrint == printNum) {
						that.looptime = 0;
						that.lastData = 0;
						that.currentTime = 1;
						that.isReceiptSend = false;
						that.isLabelSend = false;
						that.currentPrint = 1;
					} else {
						currentPrint++;
						that.currentPrint = currentPrint;
						that.currentTime = 1;
						that.Send(buff)
					}
				}
			}
		})
	},
	//票据测试
	receiptTest:function(BLEInformation,data) {
		console.log("订单信息：", data)
		var that = this;
		var canvasWidth = that.canvasWidth
		var canvasHeight = that.canvasHeight
		var command = esc.jpPrinter.createNew()
		command.init()
		// 标题
		command.bold(1); //加粗
		command.setFontSize(16); //字体大小
		command.setSelectJustification(1) //居中
		command.rowSpace(100);
		command.setText("取单号：#" + data.takeNo );
		command.setPrint();
		command.rowSpace(60);
		
		command.bold(0); //取消加粗
		command.setFontSize(0); //正常字体
		//店铺名称
		command.setText(`*${data.storeName}*`);
		command.setPrint();
		command.rowSpace(30);
		command.setPrintAndFeed(20); //打印并走纸feed个单位
		
		// 支付方式
		command.setText('--' + (data.payMode === '5'?'余额支付':'其他支付') + ' -- ');
		command.setPrint();
		command.rowSpace(30);
		command.setPrintAndFeed(20); //打印并走纸feed个单位
	
		// 订单类型
		command.setText('【' + (data.deliveryName||'') + '】');
		command.setPrint();
		command.rowSpace(30);
		command.setPrintAndFeed(20); //打印并走纸feed个单位
		
		// 预计送达时间
		command.setText('预计送达时间：' + data.serviceAt );
		command.setPrint();
		command.rowSpace(30);
		command.setPrintAndFeed(20); //打印并走纸feed个单位
		
		// 下划线
		command.setSelectJustification(1); //居中
		command.setText('------------------------------');
		command.setPrint();
		command.rowSpace(20);
		
		// 订单编号
		command.setSelectJustification(0); //居左
		command.setText('订单编号:' + data.outTradeNo);
		command.setPrint();
		command.rowSpace(20);
		command.setPrintAndFeed(5); //打印并走纸feed个单位
		
		// 下单时间
		command.setSelectJustification(0); //居左
		command.setText('下单时间:' + data.createdAt);
		command.setPrint();
		command.rowSpace(20);
		command.setPrintAndFeed(5); //打印并走纸feed个单位
		
		// 备注
		command.setSelectJustification(0); //居左
		command.setText('备注:' + data.userNote);
		command.setPrint();
		command.rowSpace(20);
		command.setPrintAndFeed(5); //打印并走纸feed个单位
		
		// 下划线
		command.setSelectJustification(1); //居中
		command.setText('------------------------------');
		command.setPrint();
		command.rowSpace(50);
		command.setPrintAndFeed(40); //打印并走纸feed个单位
		
		//列表
		command.rowSpace(80); //间距
		command.bold(5); //加粗
		command.setText("名称");
		command.setAbsolutePrintPosition(180);
		command.setText("数量");
		command.setAbsolutePrintPosition(270);
		command.setText("价格");
		command.setPrint()
		command.bold(0); //加粗
		data.goodsArr.map((good,index)=>{
			// -------index
			command.setText(good.name);
			command.setAbsolutePrintPosition(180);
			command.setText(good.num);
			command.setAbsolutePrintPosition(270);
			command.setText('￥' + good.money);
			command.setPrint()
		})
		command.setPrintAndFeed(40); //打印并走纸feed个单位
		// 其他费用
		command.setSelectJustification(1); //居中
		command.setFontSize(16); //字体大小
		command.setText('----其他费用----');
		command.setPrint();
		command.rowSpace(50);
		
		command.setSelectJustification(0); //居左
		command.bold(0); //取消加粗
		command.setFontSize(0); //字体大小
		
		command.setText('【配送费:+' + data.deliveryMoney + '】');
		command.setPrint();
		command.rowSpace(30);
		command.setPrintAndFeed(5); //打印并走纸feed个单位
		
		
		command.setText('【包装费:+' + data.boxMoney + '】');
		command.setPrint();
		command.bold(1); //加粗
		command.setPrintAndFeed(40); //打印并走纸feed个单位
		// 实付
		command.setFontSize(16); //字体大小
		command.setText('实付：￥' + data.money);
		command.setPrint();
		command.bold(0); //取消加粗
		command.setFontSize(0); //字体大小
		command.setPrintAndFeed(40); //打印并走纸feed个单位

		if(data.deliveryName==='到店自提'){
			// 下划线
			command.setSelectJustification(1); //居中
			command.setText('------------------------------');
			command.setPrint();
			command.rowSpace(20);
			
			// 结束语
			command.bold(0); //加粗
			command.setFontSize(16); //字体大小
			command.setSelectJustification(1) //居中
			command.rowSpace(100);
			command.setText('谢谢惠顾' );
			command.setPrint();
			command.rowSpace(60);
			command.setPrintAndFeed(40); //打印并走纸feed个单位
			command.setText('欢迎下次光临' );
			command.setPrint();
			command.rowSpace(60);

			command.setPrintAndFeedRow(3);
			that.prepareSend(command.getData(),BLEInformation);
		}else{
			//地址
			command.setSelectJustification(0); //居左
			command.setText('地址：' + data.receivedAddress);
			command.bold(0); //取消加粗
			command.setFontSize(16); //字体大小
			command.setPrint();

			//收货人
			command.setSelectJustification(0); //居左
			command.setText('收货人：' + data.receivedName);
			command.bold(0); //取消加粗
			command.setFontSize(16); //字体大小
			command.setPrint();
					
			// 电话
			command.setSelectJustification(0); //居左
			// command.setText(data.receivedTel.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2'));
			command.setText(data.receivedTel);
			command.setPrint();
			command.setPrintAndFeed(40); //打印并走纸feed个单位

			// 下划线
			command.setSelectJustification(1); //居中
			command.setText('------------------------------');
			command.setPrint();
			command.rowSpace(20);
			
			// 结束语
			command.bold(0); //加粗
			command.setFontSize(16); //字体大小
			command.setSelectJustification(1) //居中
			command.rowSpace(100);
			command.setText('谢谢惠顾' );
			command.setPrint();
			command.rowSpace(60);
			command.setPrintAndFeed(40); //打印并走纸feed个单位
			command.setText('欢迎下次光临' );
			command.setPrint();
			command.rowSpace(60);

			command.setPrintAndFeedRow(3);
			that.prepareSend(command.getData(),BLEInformation);
		};

		// //地址
		// command.setSelectJustification(0); //居左
		// command.setText('地址：' + data.receivedAddress);
		// // command.setText('地址：' + (data.receivedAddress==='null'?'data.receivedAddress':'到店自提'));
		// command.bold(0); //取消加粗
		// command.setFontSize(16); //字体大小
		// command.setPrint();
		
		// //收货人
		// command.setSelectJustification(0); //居左
		// // command.setText('收货人：' + (data.receivedName==='null'?'data.receivedName':'到店自提'));
		// command.setText('收货人：' + data.receivedName);
		// command.bold(0); //取消加粗
		// command.setFontSize(16); //字体大小
		// command.setPrint();
				
		// // 电话
		// command.setSelectJustification(0); //居左
		// // command.setText(data.receivedTel.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2'));
		// command.setText(data.receivedTel);
		// command.setPrint();
		// command.setPrintAndFeed(40); //打印并走纸feed个单位
		
		// // 下划线
		// command.setSelectJustification(1); //居中
		// command.setText('------------------------------');
		// command.setPrint();
		// command.rowSpace(20);
		
		// // 结束语
		// command.bold(0); //加粗
		// command.setFontSize(16); //字体大小
		// command.setSelectJustification(1) //居中
		// command.rowSpace(100);
		// command.setText('谢谢惠顾' );
		// command.setPrint();
		// command.rowSpace(60);
		// command.setPrintAndFeed(40); //打印并走纸feed个单位
		// command.setText('欢迎下次光临' );
		// command.setPrint();
		// command.rowSpace(60);

		// command.setPrintAndFeedRow(3);
		// that.prepareSend(command.getData(),BLEInformation);
	},
}
export default bleSend